import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入登录组件
import Login from '../components/Login.vue'
// 导入后台组件
import Home from '../components/Home.vue'
// 导入welcome组件
import Welcome from '../components/Welcome.vue'
// 导入user组件
import Users from '../components/user/User.vue'
// 导入Rights组件
import Rights from '../components/power/Rights.vue'
// 导入Rolse组件
import Roles from '../components/power/Roles.vue'
// 导入Cate组件
import Cate from '../components/goods/Cate.vue'
// 导入Params组件
import Params from '../components/goods/Params.vue'
// 导入List组件
import List from '../components/goods/List.vue'
// 导入Add组件
import Add from '../components/goods/Add.vue'
// 导入Order组件
import Order from '../components/orders/Order.vue'
// 导入Reports组件
import Reports from '../components/reports/Reports.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Reports }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个页面跳转而来
  // next 是一个函数，表示放行(next() 放行 next('') 强制跳转)
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
